//
//  practiceCell.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/20.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "practiceCell.h"
#import "practiceLabCell.h"
#import "practiceBtnCell.h"

@implementation practiceCell{
    CGSize labeSize;
    NSString *str;
    NSMutableDictionary *chooseDic;
    NSInteger choosetag;//单选被选中的tag值
    BOOL isChoose;//是否选择
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table registerNib:[UINib nibWithNibName:@"practiceLabCell" bundle:nil] forCellReuseIdentifier:@"practiceLabCell"];
    [self.table registerNib:[UINib nibWithNibName:@"practiceBtnCell" bundle:nil] forCellReuseIdentifier:@"practiceBtnCell"];
    chooseDic = [NSMutableDictionary dictionary];
    self.type = chooseSingle;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshCurrentData:) name:@"refreshData" object:nil];
    isChoose = NO;
}

//通知刷新
-(void)refreshCurrentData:(NSNotification *)notification{
    isChoose = NO;
    self.getChooseDic = [NSDictionary dictionaryWithDictionary:notification.userInfo];
    [self.table reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return self.chooseArr.count;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height =  [self getHeightLineWithString:str withWidth:self.frame.size.width withFont:AutoLabFont2];
    if (height < 50) {
        return 50;
    }else{
        return height;
    }
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 ) {
        //题目
        practiceLabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"practiceLabCell" forIndexPath:indexPath];
        cell.userInteractionEnabled = NO;
//        cell.content_lab.text = str;
        cell.content_lab.text = [NSString stringWithFormat:@"%ld/%ld  %@",self.currentInt+1,self.totalInt,self.dic[@"courseQuestion"]];
        return cell;
    }else{
        //选项
        practiceBtnCell *cell = [tableView dequeueReusableCellWithIdentifier:@"practiceBtnCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.choose_lab.text = self.chooseArr[indexPath.row];
        //单选
        if (self.type == chooseSingle) {
            if (([self.dic[@"currentPage"]integerValue] == _currentInt && [self.dic[@"choosetag"] integerValue] - 1000 == indexPath.row)  || ([self.getChooseDic[@"currentPage"]integerValue] == _currentInt && [self.getChooseDic[@"choosetag"] integerValue] - 1000 == indexPath.row)) {
                [cell.choose_btn setImage:[UIImage imageNamed:@"dot_icon_s"] forState:UIControlStateNormal];
            }else{
              [cell.choose_btn setImage:[UIImage imageNamed:@"dot_icon_n"] forState:UIControlStateNormal];
            }
        }
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        if (isChoose == NO && [self.getChooseDic[@"isChoose"] boolValue]== NO) {
            isChoose = YES;
            if (self.type == chooseSingle) {
                //单选 只能点击一次
                choosetag = indexPath.row + 1000;
                [self.dic setValue:[NSString stringWithFormat:@"%ld",choosetag] forKey:@"choosetag"];
                [self.dic setValue:[NSString stringWithFormat:@"%ld",_currentInt] forKey:@"currentPage"];
                [self.dic setValue:@"YES" forKey:@"isChoose"];
            }
            //点击选项 显示解析内容
            if ([self.delegate respondsToSelector:@selector(chooseSelect:currentPage:)]) {
                [self.delegate chooseSelect:self.dic currentPage:_currentInt];
            }
            [self.table reloadData];
        }
    }
}

#pragma mark - 根据字符串计算label高度
- (CGFloat)getHeightLineWithString:(NSString *)string withWidth:(CGFloat)width withFont:(UIFont *)font {
    
    //最大允许绘制的文本范围
    CGSize size = CGSizeMake(width, MAXFLOAT);
    //配置计算时的行截取方法,和contentLabel对应
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:5];
    //配置计算时的字体的大小
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:style};
    //计算
    CGFloat height = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size.height;
    
    return height;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
